import { Component, Inject } from '@angular/core';
import { IonicPage, ModalController, NavController, Refresher } from 'ionic-angular';

import { BorrowService } from '../../providers/service/borrow.service';
import { Observable } from "rxjs/Observable"
// 常量
import * as PageConstant from '../../constant/pageConstant';
/**
 * tab页面的还款
 */
@IonicPage()
@Component({
  selector: 'page-give-back-money',
  templateUrl: 'give-back-money.html'
})
export class GiveBackMoneyPage
{

  //借款进度
  private borrow$: Observable<any>;
  constructor(
    public modalCtrl: ModalController,
    private navCtrl: NavController,
    private borrowService: BorrowService,
    @Inject('auth') private authService,
  )
  {}

  /*** 初始化数据
   * */
  initData()
  {
    this.borrow$= this.borrowService.getBorrow()
      .filter(borrow=> !!borrow.progress); // 已放款
      // .map(borrow=> borrow.progress);
  }

  ionViewWillEnter()
  {
    this.initData();
  }
  ionViewDidEnter()
  {
    this.getDateFromBack(null);
  }

  /** 打开疑问
  * */
  doHelp(): void
  {
    this.navCtrl.push(PageConstant.GiveBackHelpPage);
  }

  /** 打开借钱
  * */
  doBorrowMoney(): void
  {
    //检查登录状态
    this.authService.getAuth()
      .first()
      .subscribe(auth=> {
      if(auth.hasError)
      {
        const modal = this.modalCtrl.create(PageConstant.LoginPage, {},{
          showBackdrop: false,
          enableBackdropDismiss: false,
        });
        modal.present();
        return;
      }
      this.navCtrl.parent.select(0);
    });
  }

  /** 打开订单详情
  * */
  doOrderDetails(event)
  {
    // const newProgress= event.value.progress; // 最近的progress
    // const newObj= newProgress[0];
    // let item; // 得到当前激活的数据
    // debugger;
    // this.ordersSubscription=
    //   this.ordersService.getOrders()
    //     .filter(orders=> !!orders.orderList)
    //     .map(data=> data.orderList
    //   ).subscribe(list=>{
    //     list.forEach(value=>{
    //       let bool= false;
    //       value.progress.forEach(value=>{
    //         if(value.describe===newObj.describe
    //           && value.data===newObj.data
    //         ) {bool= true;}
    //       });
    //       if(bool){
    //         debugger;
    //         this.isCanNextBool= true;
    //         item= {...value};
    //       }
    //     })
    //   });
    // // 若下拉未完成, 不能下一步
    // if(!this.isCanNextBool) return;
    // const modal = this.modalCtrl.create(PageConstant.OrderDetailsPage, {
    //   data: item
    // },{
    //   showBackdrop: false,
    //   enableBackdropDismiss: false,
    // });
    // modal.onDidDismiss(data => {
    //   debugger;
    // });
    // modal.present();
  }

  /** 打开还款方式
  * */
  doGiveBackType()
  {
    this.navCtrl.push(PageConstant.GiveBackTypePage);
  }
  /*** 获取订单数据
  * */
  private getDateFromBack(refresher: any)
  {
    //检查登录状态
    this.authService.getAuth()
      .first()
      .subscribe(auth=> {
        if(auth.hasError)
        {
          if(refresher) refresher.complete();
          return;
        }
        this.borrowService.getUserBorrowProgress(refresher);
      });
  }

  /*** 上拉刷新
   * */
  executeRefresh(refresher: Refresher)
  {
    // this.isCanNextBool= false; // 初始化开关
    this.getDateFromBack(refresher);
  }

}
